//获取应用实例
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    curNewsId:'',
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    app.setNavColor(that);
    setTimeout(function () {
      that.requestServiceFl();
    }, 1)
  },
  onShow: function () {
    var that = this;
    app.setNavtitle('全部预约');
    that.setData({
      customerService: app.globalData.customerService ? app.globalData.customerService:'',
      sessionForm: app.globalData.sessionForm ? app.globalData.sessionForm:''
    })
  },
  requestServiceFl: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_appointment_good_kind'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            newsTab: res.data.data,
          })
          if (!that.data.curNewsId) {
            that.setData({
              curNewsId: res.data.data&&res.data.data.length > 0 ? res.data.data[0].id : '',
              goIntoView: res.data.data && res.data.data.length > 0 ? 'newsfl' + res.data.data[0].id:'',
            })
          }
          var cid = that.data.curNewsId;
          that.requestServiceList(cid);
        } else {
          that.setData({
            newsTab: []
          })
          that.requestServiceList(0);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  newsToggle: function (e) {
    var that = this;
    var id = e.target.dataset.id;
    that.setData({
      page: 0,
      curNewsId: id,
      goIntoView: 'newsfl' + id,
      noMoretip: false,
      showLoading: true
    })
    if (that.data.projectList.length <= 0) {
      that.setData({
        projectList: null
      })
    }
    that.requestServiceList(id);
  },
  requestServiceList: function (cid) {
    var that = this;
    var data = {};
    var page = that.data.page;
    var type = that.data.type;
    var sortType = that.data.sortType;
    data.map = 'applet_appointment_good_list';
    data.kind = cid;
    data.page = page;
    app.setVersion(that);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.projectList ? that.data.projectList:[];
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            projectList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.projectList);
        } else {
          if (page <= 0) {
            console.log(page+'---');
            that.setData({
              projectList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            console.log(page + '1111');
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    var cid = that.data.curNewsId;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    if (that.data.projectList.length <= 0) {
      that.setData({
        projectList: null
      })
    }
    that.requestServiceFl();
    that.requestServiceList(cid);
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    var cid = that.data.curNewsId;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestServiceList(cid);
    }
  },
  openDetail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages/Generalreservationdetail/Generalreservationdetail?id=' + id
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var shareInfo = app.globalData.shareInfo;
    var title = shareInfo.shareTitle ? shareInfo.shareTitle : '全部预约';
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/subpages/GeneralreservationProject/GeneralreservationProject?enterfrom=share'
    }
  }
})
